package com.kx.core.dawn.dbs.dao;

import com.genchance.core.dawn.entity.BaseBean;
import com.kx.game.center.dbs.DBServices;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

public class BaseDao {

    public String tableName;

    public <T extends BaseBean> T get(String serverId, String key) {
        SqlSession dbConntion = null;
        try {
            dbConntion = DBServices.getDBConntion(serverId);
            return (T) dbConntion.selectOne(this.tableName + ".get", key);
        } finally {
            if (dbConntion != null)
                dbConntion.close();
        }
    }

    public <T extends BaseBean> T get(String key) {
        return get(DBServices.myServerId, key);
    }

    public <T extends BaseBean> T get(String serverId, Long key) {
        SqlSession dbConntion = null;
        try {
            dbConntion = DBServices.getDBConntion(serverId);
            return (T) dbConntion.selectOne(this.tableName + ".get", key);
        } finally {
            if (dbConntion != null)
                dbConntion.close();
        }
    }

    public <T extends BaseBean> void insert(String serverId, T baseBean) {
        SqlSession dbConntion = null;
        try {
            dbConntion = DBServices.getDBConntion(serverId);
            dbConntion.insert(this.tableName + ".insert", baseBean);
        } finally {
            if (dbConntion != null)
                dbConntion.close();
        }
    }

    public <T extends BaseBean> void insert(T baseBean) {
        insert(DBServices.myServerId, baseBean);
    }

    public <T extends BaseBean> void update(String serverId, T baseBean) {
        SqlSession dbConntion = null;
        try {
            dbConntion = DBServices.getDBConntion(serverId);
            dbConntion.update(this.tableName + ".update", baseBean);
        } finally {
            if (dbConntion != null)
                dbConntion.close();
        }
    }

    public <T extends BaseBean> void update(T baseBean) {
        update(DBServices.myServerId, baseBean);
    }

    public <T extends BaseBean> void delete(String serverId, T baseBean) {
        SqlSession dbConntion = null;
        try {
            dbConntion = DBServices.getDBConntion(serverId);
            dbConntion.update(this.tableName + ".delete", baseBean);
        } finally {
            if (dbConntion != null)
                dbConntion.close();
        }
    }

    public <T extends BaseBean> void delete(T baseBean) {
        delete(DBServices.myServerId, baseBean);
    }


    public <T extends BaseBean> List<T> findAll(String serverId) {
        SqlSession dbConntion = null;
        try {
            dbConntion = DBServices.getDBConntion(serverId);
            return dbConntion.selectList(this.tableName + ".findAll");
        } finally {
            if (dbConntion != null)
                dbConntion.close();
        }
    }

    public <T extends BaseBean> List<T> findAll() {
        return findAll(DBServices.myServerId);
    }
}
